package mail;

import javax.annotation.Resource;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.mail.Address;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
@Singleton  
@Startup  
public class EmailService {  
	
	@Resource(mappedName="java:jboss/mail/Default")
	private Session mailSession;
	//metodo para enviar correo electronico
		public void enviarEmail(String asunto, String destinatario, String texto){
			try    {
				
	            MimeMessage m = new MimeMessage(mailSession);
	            Address from = new InternetAddress("usr3@ups.com");
	            Address[] to = new InternetAddress[] {new InternetAddress(destinatario) };
	            m.setFrom(from);
	            m.setRecipients(javax.mail.Message.RecipientType.TO, to);
	            m.setSubject(asunto);
	            m.setSentDate(new java.util.Date());
	            texto = texto.replaceAll("\n", "<br/>");
	            m.setContent(texto,"text/html; charset=utf-8");
	            Transport.send(m);
	            System.out.println("Correo enviado");
	        }
	        catch (javax.mail.MessagingException e)       
	        {
	            e.printStackTrace();
	        }
		}
}  